import React, { Component } from 'react';
import { Form, Input, Select } from 'antd';
import './modal.css';
import PropTypes from 'prop-types';
import {reqOrganListByParentOrgid2} from '../../api';
import { connect } from 'react-redux';

const { Option } = Select;

class AddWorkshopForm extends Component {

  constructor(props){
    super(props);
    this.state = {
      organList:[] 
    }
  }

  static propTypes = {
    setForm: PropTypes.func.isRequired
  }

  formRef = React.createRef();

  getOrganList = async() => {  // 获取用户权限内的全部组织
    const org_pid = this.props.user.org.org_id;
    this.setState({ loading: true })
    const result = await reqOrganListByParentOrgid2(org_pid);
    this.setState({loading: false});
    const organList = result.data;  
    this.setState({
      organList
    })
  }

  componentDidMount() {
    this.props.setForm(this.formRef);
    this.getOrganList();
  }
  render() {
    const layout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 16 },
    };
    return (
      <div>
        <Form 
          {...layout} 
          ref={this.formRef}   
          name="control-ref" 
          onFinish={this.onFinish}
        >
          <Form.Item 
            name="ws_name" 
            label="部门名称" 
            rules={[{ required: true }]}
          >
            <Input placeholder="请输入部门名称"/>
          </Form.Item>

          <Form.Item 
            name="org_id" 
            label="所属矿区" 
            rules={[{ required: true }]}
          >
            <Select
              placeholder="请选择所属矿区"
              allowClear
            >
              {this.state.organList.map((item) => <Option value={item.org_id}>{item.org_name}</Option>)}
            </Select>
          </Form.Item>

          <Form.Item
            name="ws_address"
            label="部门地址"
            rules={[{ required: true, message: '请输入部门地址!' }]}
          >
            <Input placeholder="请输入部门地址"/>
          </Form.Item>

          <Form.Item
            name="ws_contact"
            label="部门联系方式"
            rules={[{ required: true, message: '请输入部门联系方式!' }]}
          >
            <Input placeholder="请输入部门联系方式"/>
          </Form.Item>

          <Form.Item name="ws_remark" label="备注">
            <Input.TextArea />
          </Form.Item>
        </Form>
      </div>
      
    )
  }
}

export default connect(
  state => ({user: state.user}),
  {}
)(AddWorkshopForm);